﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SharpDiary.Core.Controls
{
    public partial class PageButtonGroup : UserControl
    {
        private long _currentPageNumber = 1;

        /// <summary>
        /// 获取或设置当前页码。
        /// </summary>
        public long CurrentPageNumber
        {
            get { return _currentPageNumber; }
            set
            {
                if (value < 0 || value > _pageCount) { return; }

                _currentPageNumber = value;

                btnFirst.Enabled = _currentPageNumber != 1;
                btnPrior.Enabled = _currentPageNumber - 1 > 0;
                btnNext.Enabled = _currentPageNumber + 1 <= _pageCount;
                btnLast.Enabled = _currentPageNumber != _pageCount;

                Application.DoEvents();

                OnPageNumberChanged(this, new PageNumberChangedEventArgs(value));
            }
        }

        private bool _autoRefreshButtonsEnabledState = true;

        /// <summary>
        /// 获取或设置是否启用自己更新翻页按钮的可用状态。
        /// </summary>
        public bool AutoRefreshButtonsEnabledState
        {
            get { return _autoRefreshButtonsEnabledState; }
            set
            {
                _autoRefreshButtonsEnabledState = value;
                if (value) { timerMain.Start(); }
            }
        }

        private long _pageCount = 0;

        /// <summary>
        /// 获取或设置总页数。
        /// </summary>
        public long PageCount
        {
            get { return _pageCount; }
            set
            {
                _pageCount = value;
                _currentPageNumber = 1; //不要触发 OnPageNumberChange 事件
                
                btnFirst.Enabled = _currentPageNumber != 1;
                btnPrior.Enabled = _currentPageNumber - 1 > 0;
                btnNext.Enabled = _currentPageNumber + 1 <= _pageCount;
                btnLast.Enabled = _currentPageNumber != _pageCount;


            }
        }

        /// <summary>
        /// 当前页码发生改变后触发此事件。
        /// </summary>
        public event EventHandler<PageNumberChangedEventArgs> PageNumberChanged;

        protected virtual void OnPageNumberChanged(Object sender, PageNumberChangedEventArgs e)
        {
            if (PageNumberChanged != null) { PageNumberChanged(sender, e); }
        }

        /// <summary>
        /// 更新翻页按钮的可用状态。
        /// </summary>
        public void RefreshButtonsEnableState()
        {
            btnFirst.Enabled = _currentPageNumber != 1;
            btnPrior.Enabled = _currentPageNumber - 1 > 0;
            btnNext.Enabled = _currentPageNumber + 1 <= _pageCount;
            btnLast.Enabled = _currentPageNumber != _pageCount;

            Application.DoEvents();
        }

        #region Constructors

        public PageButtonGroup()
        {
            InitializeComponent();

            ResizeRedraw = true;
            DoubleBuffered = true;
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (_autoRefreshButtonsEnabledState) { timerMain.Start(); }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            CurrentPageNumber = 1;
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            CurrentPageNumber = PageCount;
        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            CurrentPageNumber = CurrentPageNumber - 1;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            CurrentPageNumber = CurrentPageNumber + 1;
        }

        private void timerMain_Tick(object sender, EventArgs e)
        {
            RefreshButtonsEnableState();
        }

    }

    public class PageNumberChangedEventArgs : EventArgs
    {
        private long _currentPageNumber = 0;

        public long CurrentPageNumber
        {
            get { return _currentPageNumber; }
            set { _currentPageNumber = value; }
        }

        public PageNumberChangedEventArgs() { }

        public PageNumberChangedEventArgs(long pageNumber)
        {
            _currentPageNumber = pageNumber;
        }
    }
}
